﻿using CommandLine;
using CommandLine.Text;

namespace PBIW.Console
{
    internal class SentenceBuilderInChinese : SentenceBuilder
    {
        public override Func<string> RequiredWord => () => "[必选]";

        public override Func<string> ErrorsHeadingText => () => "指令错误：";

        public override Func<string> UsageHeadingText => () => "用法：";

        public override Func<string> OptionGroupWord => () => "选项组";

        public override Func<bool, string> HelpCommandText => (bool isOption) => isOption ? "显示此帮助。" : "显示此帮助。";

        public override Func<bool, string> VersionCommandText => (bool isOption) => isOption ? "显示此程序版本信息。" : "显示版本信息。";

        public override Func<Error, string> FormatError => delegate (Error error)
        {
            switch (error.Tag)
            {
                case ErrorType.BadFormatTokenError:
                    return $"无法识别 {((BadFormatTokenError)error).Token} 。";
                case ErrorType.MissingValueOptionError:
                    return $"选项 {((MissingValueOptionError)error).NameInfo.NameText} 没有值。";
                case ErrorType.UnknownOptionError:
                    return $"未知选项 {((UnknownOptionError)error).Token} 。";
                case ErrorType.MissingRequiredOptionError:
                    {
                        MissingRequiredOptionError missingRequiredOptionError = (MissingRequiredOptionError)error;
                        if (!missingRequiredOptionError.NameInfo.Equals(NameInfo.EmptyName))
                        {
                            return $"必选项 {missingRequiredOptionError.NameInfo.NameText} 缺少值。";
                        }

                        return $"有一未绑定名称的必选项缺少值。";
                    }
                case ErrorType.BadFormatConversionError:
                    {
                        BadFormatConversionError badFormatConversionError = (BadFormatConversionError)error;
                        if (!badFormatConversionError.NameInfo.Equals(NameInfo.EmptyName))
                        {
                            return $"必选项 {badFormatConversionError.NameInfo.NameText} 的值无法识别。";
                        }

                        return $"有一未绑定名称的必选项的值无法识别。";
                    }
                case ErrorType.SequenceOutOfRangeError:
                    {
                        SequenceOutOfRangeError sequenceOutOfRangeError = (SequenceOutOfRangeError)error;
                        if (!sequenceOutOfRangeError.NameInfo.Equals(NameInfo.EmptyName))
                        {
                            return $"为 {sequenceOutOfRangeError.NameInfo.NameText} 传入的值过少。";
                        }

                        return $"有一未绑定名称的需要传入一系列数据的选项，其传入的值数量过少。";
                    }
                case ErrorType.BadVerbSelectedError:
                    return $"无法识别指令 {((BadVerbSelectedError)error).Token} 。";
                case ErrorType.NoVerbSelectedError:
                    return $"没有输入指令。";
                case ErrorType.RepeatedOptionError:
                    return $"选项 {((RepeatedOptionError)error).NameInfo.NameText} 有重复。";
                case ErrorType.SetValueExceptionError:
                    {
                        SetValueExceptionError setValueExceptionError = (SetValueExceptionError)error;
                        return $"为选项 {setValueExceptionError.NameInfo.NameText} 设置值时发生错误：{setValueExceptionError.Exception.Message}";
                    }
                case ErrorType.MissingGroupOptionError:
                    {
                        MissingGroupOptionError missingGroupOptionError = (MissingGroupOptionError)error;
                        return $"至少需要为选项组 {missingGroupOptionError.Group} （ ${string.Join(" 、 ", missingGroupOptionError.Names.Select((NameInfo n) => n.NameText))} ）中的一个选项设置值。";
                    }
                case ErrorType.GroupOptionAmbiguityError:
                    {
                        GroupOptionAmbiguityError groupOptionAmbiguityError = (GroupOptionAmbiguityError)error;
#warning 此翻译不知道正确不正确
                        return $"不能为选项 {groupOptionAmbiguityError.Option.NameText} 同时使用组和名称。";
                        // return "Both SetName and Group are not allowed in option: (".JoinTo(groupOptionAmbiguityError.Option.NameText, ")");
                    }
                case ErrorType.MultipleDefaultVerbsError:
                    return $"不能同时有多个默认指令。";
                default:
                    throw new InvalidOperationException();
            }
        };

        public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors => delegate (IEnumerable<MutuallyExclusiveSetError> errors)
        {
            var bySet = from e in errors
                        group e by e.SetName into g
                        select new
                        {
                            SetName = g.Key,
                            Errors = g.ToList()
                        };
            string[] value = bySet.Select(set =>
            {
                string text = string.Join(string.Empty, set.Errors.Select((MutuallyExclusiveSetError e) => $"{e.NameInfo.NameText} , ").ToArray());
                int num = set.Errors.Count;
                string text2 = string.Join(string.Empty, (from x in (from s in bySet
                                                                     where !s.SetName.Equals(set.SetName)
                                                                     from e in s.Errors
                                                                     select e).Distinct()
                                                          select $"{x.NameInfo.NameText} ， ").ToArray());
                return $"选项 {text[0..^2]}与 {text2[0..^2]}不匹配。";
            }).ToArray();
            return string.Join(Environment.NewLine, value);
        };
    }
}
